package cn.itsub.boot.controller;

import cn.itsub.boot.model.Category;
import cn.itsub.boot.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
public class ViewController {

    @Autowired
    CategoryService categoryService;

    @RequestMapping({"/","/index.html"})
    public String index(Model model) {
        List<Category> categoryList = categoryService.findByParentId(0);
        model.addAttribute("categoryList", categoryList);
        return "index"; //index.ftl
    }

    @RequestMapping({"/course/explore"})
    public String courseExplore(
            @RequestParam(value = "cid",defaultValue = "0") Integer cid,
            @RequestParam(value = "child",defaultValue = "0") Integer child,
            Model model) {
        List<Category> categoryList = categoryService.findByParentId(0);
        model.addAttribute("categoryList", categoryList);
        model.addAttribute("cid", cid);
        model.addAttribute("child", child);
        //筛选二级类别
        for(Category category:categoryList){
            if(category.getId().equals(cid)){
                model.addAttribute("childList", category.getChildren());
            }
        }
        return "course/explore";
    }

    @RequestMapping({"/login.html"})
    public String loginPage() {
        return "login"; //login.ftl
    }

    @RequestMapping({"/register.html"})
    public String registerPage() {
        return "register"; //login.ftl
    }


}
